package org.lanqiao.api.cart;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.common.Constants;
import org.lanqiao.common.JsonResult;
import org.lanqiao.common.JsonResultWriter;
import org.lanqiao.pojo.CartListPOJO;
import org.lanqiao.service.CartServiceImpl;

/**
 * 修改商品数量的接口
 * @author dzp
 *
 */
@WebServlet("/api/cart/update")
public class UpdateCartServlet extends HttpServlet {

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//传入参数，传入购物车编号carId,修改商品数量同时商品总价也会增加
	    String id = request.getParameter("CarId");
	    String count = request.getParameter("ProductCount");
	    String userId = request.getParameter("UserId");
	    String totalPrice = request.getParameter("TotalPrice");
		JsonResult result=null;
		
		CartServiceImpl removeCartService = new CartServiceImpl();	
		CartListPOJO cartlist = new CartListPOJO();
		cartlist.setId(id);
		cartlist.setProductCount(count);
		cartlist.setTotalPrice(totalPrice);
		List list = removeCartService.update(cartlist,userId);
		try {
			if(list.size()>0){				
				result=new JsonResult("修改成功",Constants.STATUS_SUCCESS,list);
			}else {				
				result=new JsonResult("修改失败",Constants.STATUS_UNFOUND );
			}
		} catch (Exception e) {			
			result=new JsonResult( "修改数量异常",Constants.STATUS_FAILURE);
		}
		
		JsonResultWriter.writer(response, result);
	}

}
